package com.yuange.utils.db.mysql;

import com.alibaba.fastjson.JSONObject;
import com.yuange.utils.common.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @作者：工号308、袁智彪
 * @时间：2022/5/25 9:18
 */
public class MySQLUtils {

    static Logger log = LoggerFactory.getLogger(MySQLUtils.class);

    public static void main(String[] args) {
        String sql = "show tables;";
        List<JSONObject> list = currencyMYSQL(sql);
        for (JSONObject object : list) {
            System.out.println(object);
        }
    }

    /**
     * 通用的MYSQL方法
     * @param sql
     * @return
     */
    public static List<JSONObject> currencyMYSQL(String sql) {
        Connection cn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            cn = MySQLDruidDataPool.getConn();
            ps = cn.prepareStatement(sql);
            rs = ps.executeQuery();
            List<JSONObject> list = CommonUtil.convertList(rs);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            MySQLDruidDataPool.close(cn,ps,rs);
        }
    }

}
